<?php defined('SYSPATH') or die('No direct script access.');

	class Model_Services_conclusion extends Model implements Jqgrid_GridListener{
	
		public $conclusion;
		
		public function __construct($id=false){
			if ($id) $this->conclusion = $this->get($id);
		}	
		
		public function count($criteria = false){ return ORM::factory("dao_conclusion")->count_all();}
			
		public function getList($criteria = false){
			
			if (!$criteria)	$conclusion_list = ORM::factory("dao_conclusion")->find_all()->as_array();
			else{
				if ($criteria->isSearch){
					$conclusion_list = ORM::factory("dao_conclusion")
									->where($criteria->getSQLField("conclusions"),$criteria->getSQLOper(), $criteria->getSQLSearchString())
									->order_by($criteria->sidx,$criteria->sord)
									->limit($criteria->limit)
									->offset($criteria->calcOffset())
									->find_all()
									->as_array();
							
				}else{
					$conclusion_list = ORM::factory("dao_conclusion")->order_by($criteria->sidx,$criteria->sord)->limit($criteria->limit)->offset($criteria->calcOffset())->find_all()->as_array(); 
				}
			} 
		
			$dataset = array();
			foreach ($conclusion_list as $conclusion){
				$dataset[$conclusion->id] = array($conclusion->conclusion_name,$conclusion->conclusion,$conclusion->male,$conclusion->female);
			}
			
			return $dataset;
		}
	
		public function get($id){ return ORM::factory("dao_conclusion")->find($id);}
		public function getAll(){return ORM::factory("dao_conclusion")->find_all()->as_array();}
		public function getMale(){return ORM::factory("dao_conclusion")->where("female","=","0")->where("male","=","1")->find_all()->as_array();}
		public function getFemale(){return ORM::factory("dao_conclusion")->where("female","=","1")->where("male","=","0")->find_all()->as_array();}
						
		public function save($id, array $data){
			$this->conclusion = ($id > 0) ? ORM::factory("dao_conclusion",$id) : ORM::factory("dao_conclusion");
			
			$this->conclusion->conclusion_name = $data['conclusion_name'];
			$this->conclusion->conclusion = $data['conclusion'];
			$this->conclusion->female = $data['female'];
			$this->conclusion->male = $data['male'];
								
			$this->conclusion->save();
			return $this->conclusion->id." guardado con exito";
	    }
		
		public function delete($id){
			$conclusion = ORM::factory("dao_conclusion",$id);
			$conclusion->delete();
			return "Conclusion ".$id." eliminada con exito";
		}
	}
?>